package cn.chencq.demo.publish;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.chencq.demo.MQInit;
import cn.chencq.demo.constant.MqConfig;
import cn.chencq.mq.MQUtil;
import cn.chencq.mq.MQUtil.ExchangeType;

/**
 * 发送消息
 * 
 * @author chencq
 * 
 */
public class MqPublish {

	private static final Logger LOGGER = LoggerFactory.getLogger(MqPublish.class);

	// 发送消息exchange名称
	private static final String EXCHANGE = MqConfig.getExchange();

	private static final String CLUSTER = MqConfig.getClusterName();

	private static final String ROUTING_KEY = MqConfig.getRoutingKey();

	static {
		try {

			MQInit.init();

			LOGGER.info("MQPublish 初始化");

			MQUtil.exchangeDeclare(CLUSTER, EXCHANGE, ExchangeType.DIRECT);

		} catch (Exception e) {
			LOGGER.error("MQPublish 初始化异常", e);
			e.printStackTrace();
		}
	}

	public static void sendMsg(String msg) {
		try {

			MQUtil.sendMsg(CLUSTER, EXCHANGE, ROUTING_KEY, msg.getBytes());

			LOGGER.info("Publish Msg : {}", msg);
		} catch (Exception e) {
			LOGGER.error("发送消息异常, Msg:{}", msg, e);
			e.printStackTrace();
		}

	}
}
